package com.example.myspace.control;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myspace.consumer.WebSocketServerChat;
import com.example.myspace.mapper.UserMapper;
import com.example.myspace.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class userlist {
    @Autowired
    UserMapper userMapper;

    @GetMapping("/myspace/user/userlist/")
    public JSONObject getUser(@RequestParam Map<String, String> data){
        Integer page = Integer.valueOf(data.get("page"));
        IPage<User> IPage = new Page<>(page, 8);
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        List<User> userList = userMapper.selectPage(IPage, queryWrapper.orderByAsc("id")).getRecords();
        List<JSONObject> items = new ArrayList<>();
        for(User user : userList){
            JSONObject item = new JSONObject();
            if(WebSocketServerChat.users.get(user.getId()) != null){
                item.put("user_status", true);
            }else{
                item.put("user_status", false);
            }
            item.put("user", user);
            items.add(item);
        }
        JSONObject resp = new JSONObject();
        resp.put("total_count", userMapper.selectCount(null));
        resp.put("users", items);
        return  resp;
    }
}
